package avicit.bdp.dgs.mdm.utils.api.request;

import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PostTest {

    private static RestTemplate restTemplate;

    public static void main(String[] args){
        get();
    }

    public static void post(){
        try{
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(80 * 1000);
            requestFactory.setReadTimeout(80 * 1000);
            restTemplate = new RestTemplate(requestFactory);
            List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
            messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
            messageConverters.add(new FormHttpMessageConverter());

            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
            converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
            messageConverters.add(converter);
            restTemplate.getMessageConverters().addAll(messageConverters);

            String url = "http://10.216.38.71:9090/bdp/api/bdp/dgs/mdm/monitor/getDataList";
            String data = "{\"user\":[{\"optType\":2,\"data\":\"{\\\"birthday\\\":\\\"2023-12-20 00:00:00\\\",\\\"code\\\":\\\"aaa000000000190bbb\\\",\\\"sys_last_updated_by\\\":\\\"1\\\",\\\"sys_table_id\\\":\\\"8a58a6c78c80122a018c866688d304db\\\",\\\"name\\\":\\\"2\\\",\\\"sys_creation_date\\\":\\\"2023-12-21 14:29:54\\\",\\\"sys_data_id\\\":\\\"8a58a6c78c8b0fb6018c8b10f52c000d\\\",\\\"sys_last_update_date\\\":\\\"2023-12-21 15:32:43\\\",\\\"age\\\":2,\\\"sys_created_by\\\":\\\"1\\\",\\\"sys_org_identity\\\":\\\"ORG_ROOT\\\",\\\"sys_status\\\":0}\"}]}";
            // 创建一个头部对象
            HttpHeaders headers = new HttpHeaders();
            List<MediaType> mediaTypeList = new ArrayList<>();
            mediaTypeList.add(MediaType.ALL);
            headers.setAccept(mediaTypeList);
            headers.add("applicationCode", "1");
            headers.add("authCode", "2");
            headers.add("logId", "3");
            headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
            HttpEntity<Object> httpEntity = new HttpEntity<Object>(data, headers);

            ResponseEntity<String> result = restTemplate.postForEntity(url, httpEntity , String.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void get(){
        try{
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(80 * 1000);
            requestFactory.setReadTimeout(80 * 1000);
            restTemplate = new RestTemplate(requestFactory);
            List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
            messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
            messageConverters.add(new FormHttpMessageConverter());

            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
            converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
            messageConverters.add(converter);
            restTemplate.getMessageConverters().addAll(messageConverters);

            String url = "http://10.216.38.71:9090/bdp/api/bdp/dgs/mdm/monitor/callback?logId="+100+"&status=1";
            // 创建一个头部对象
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
            List<MediaType> mediaTypeList = new ArrayList<>();
            mediaTypeList.add(MediaType.ALL);
            headers.setAccept(mediaTypeList);
            headers.add("applicationCode", "1");
            headers.add("authCode", "2");
            headers.add("logId", "3");
            HttpEntity httpEntity = new HttpEntity<Object>("", headers);

            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
